package com.admin.service;

import com.admin.dao.StorageDao;
import com.admin.entity.Storage;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.admin.sys.utils.admin.StringUtils;
import com.admin.sys.utils.admin.ObjectUtils;
import com.admin.sys.base.module.extend.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;

//入库信息接口类
@Service
public class StorageService extends BaseService {

    @Autowired
    private StorageDao storageDao;

    //【入库信息】设置查询条件
    private LambdaQueryWrapper<Storage> getStorageQueryCondition(Storage storage) {
        LambdaQueryWrapper<Storage> lambdaQuery = this.getBaseQueryCondition(storage);
        //根据创建时间排序
        lambdaQuery.orderByDesc(Storage::getCreateDate);
        if (ObjectUtils.isNotNull(storage)) {
            //【主键id】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(storage.getId()), Storage::getId, storage.getId());
            //【供应商名称】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(storage.getSupplierId()), Storage::getSupplierId, storage.getSupplierId());
            //【物品名称】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(storage.getGoodsId()), Storage::getGoodsId, storage.getGoodsId());
            //【入库数量】模糊查询
            lambdaQuery.like(StringUtils.isNotNull(storage.getNumber()), Storage::getNumber, storage.getNumber());
            //【入库时间】精确查询
            lambdaQuery.eq(ObjectUtils.isNotNull(storage.getStorageTime()), Storage::getStorageTime, storage.getStorageTime());
            //【入库时间】范围查询
            if (ObjectUtils.isNotNull(storage.getStorageTimeRange()) && storage.getStorageTimeRange().size() > 0) {
                lambdaQuery.between(Storage::getStorageTime, storage.getStorageTimeRange().get(0), storage.getStorageTimeRange().get(1));
            }
            //【操作人】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(storage.getUserId()), Storage::getUserId, storage.getUserId());
        }
        return lambdaQuery;
    }

    //【入库信息】分页查询
    public IPage<Storage> getPage(Page<Storage> page, Storage storage) {
        LambdaQueryWrapper<Storage> lambdaQuery = getStorageQueryCondition(storage);
        return storageDao.selectPage(page, lambdaQuery);
    }
    
    //【入库信息】查询列表
    public List<Storage> getList(Storage storage) {
        LambdaQueryWrapper<Storage> lambdaQuery = getStorageQueryCondition(storage);
        return storageDao.selectList(lambdaQuery);
    }
    
    //【入库信息】根据id查询
    public Storage get(String id) {
        return storageDao.selectById(id);
    }

    //【入库信息】根据对象查询
    public Storage get(Storage storage) {
    LambdaQueryWrapper<Storage> lambdaQuery = getStorageQueryCondition(storage);
        return storageDao.selectOne(lambdaQuery);
    }
    
    //【入库信息】新增
    public int insert(Storage storage) {
        this.preInsert(storage);
        return storageDao.insert(storage);
    }
    
    //【入库信息】修改
    public int update(Storage storage) {
        this.preUpdate(storage);
        return storageDao.updateById(storage);
    }
    
    //【入库信息】删除
    public int delete(String id) {
        return storageDao.deleteById(id);
    }

    //【入库信息】批量删除
    public int delAll(String[] ids) {
        return storageDao.deleteBatchIds(Arrays.asList(ids));
    }

}